# -*- coding: utf-8 -*-
# author yzs
# date 2019-01-01
#
# 链表区间逆序
# Description
# 将单个链表的每个K节点之间逆序，打印出新链表；最后不足K的节点数不需要逆序;要求时间复杂度为O(n),额外空间复杂度为O(1)
# Input
# 输入的每一行的值用空格隔开，第一个表示链表长度，中间为节点值，最后代表k
# Output
# 输出的每一行为新的链表，节点值用空格隔开，末尾不要空格
# Sample Input 1
# 8 1 2 3 4 5 6 7 8 3
# 8 a b c d e f g h 4
# Sample Output 1
# 3 2 1 6 5 4 7 8
# d c b a h g f e


class Node(object):
    def __init__(self, data, p=0):
        self.data = data
        self.next = p


class LinkList(object):
    def __init__(self):
        self.head = 0

    def init(self, data):
        if data[0] is not '-1':
            self.head = Node(data[0])
        p = self.head
        data_len = len(data)
        for i in range(1, data_len):
            if data[i] == '-1':
                break
            else:
                p.next = Node(data[i])
                p = p.next
        p.next = None

    def print(self, head):
        p = head
        while p.next is not None:
            print(p.data, end=' ')
            p = p.next
        print(p.data)


def reverse(head, left, right):
    pre = None
    start = head
    while head != right:
        next = head.next
        head.next = pre
        pre = head
        head = next
    if left is not None:
        left.next = pre
    start.next = right


def reverse_k_node(head, k):
    if head is None or head.next is None or k < 2:
        return head
    pre = None
    current = head
    count = 0
    while current is not None:
        count += 1
        next = current.next
        if count == k:
            if pre is None:
                start = head
                head = current
            else:
                start = pre.next
            reverse(start, pre, next)
            pre = start
            count = 0
        current = next
    return head


while True:
    try:
        data = list(input().strip().split())
        data, k = data[1:int(data[0]) + 1], int(data[int(data[0]) + 1])
        link_list = LinkList()
        link_list.init(data)
        head = reverse_k_node(link_list.head, k)
        link_list.print(head)
    except EOFError:
        break
